package com.baide.sanguanheyi.swing;

import com.baide.sanguanheyi.sqlite.Content;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import javax.swing.*;
import javax.swing.table.TableModel;
import java.io.*;
import java.util.List;

public class ExcelExporter {
    /**
     * 导出JTable到excel
     */
    public void exportTable(JTable table, File file) throws IOException {
        TableModel model = table.getModel();
        BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));
        for (int i = 0; i < model.getColumnCount(); i++) {
            bWriter.write(model.getColumnName(i));
            bWriter.write("\t");
        }
        bWriter.newLine();
        for (int i = 0; i < model.getRowCount(); i++) {
            for (int j = 0; j < model.getColumnCount(); j++) {
                if (j == 1 || j == 2) {
                    bWriter.write(" " + model.getValueAt(i, j).toString());
                } else {
                    bWriter.write(model.getValueAt(i, j).toString());
                }
                bWriter.write("\t");
            }
            bWriter.newLine();
        }
        bWriter.close();
    }

    public void export(File file, List<Content> data) {
        HSSFWorkbook wb = null;
        try {
            InputStream resourceAsStream = this.getClass().getResourceAsStream("/export/9111.xls");
            POIFSFileSystem fs = new POIFSFileSystem(resourceAsStream);
            // 读取excel模板
            wb = new HSSFWorkbook(fs);
            // 读取了模板内所有sheet内容
            HSSFSheet sheet = wb.getSheetAt(0);
            // 在相应的单元格进行赋值
            int rowIndex = 1;
            for (Content d : data) {
                HSSFRow row = sheet.getRow(rowIndex);
                if (null == row) {
                    row = sheet.createRow(rowIndex);
                }
                HSSFCell cell0 = row.getCell(0);
                if (null == cell0) {
                    cell0 = row.createCell(0);
                }
                cell0.setCellValue(d.getSEND_NAME());// 寄件人
                HSSFCell cell1 = row.getCell(1);
                if (null == cell1) {
                    cell1 = row.createCell(1);
                }
                cell1.setCellValue(d.getSENDER_TEL());// 寄件人电话
                HSSFCell cell2 = row.getCell(2);
                if (null == cell2) {
                    cell2 = row.createCell(2);
                }
                cell2.setCellValue(d.getSENDER_ADDR());// 寄件人地址

                HSSFCell cell3 = row.getCell(3);
                if (null == cell3) {
                    cell3 = row.createCell(3);
                }
                cell3.setCellValue(d.getVoyageNo());// 总邮件号
                HSSFCell cell4 = row.getCell(4);
                if (null == cell4) {
                    cell4 = row.createCell(4);
                }
                cell4.setCellValue(d.getBarCode());// 邮件号
                HSSFCell cell5 = row.getCell(5);
                if (null == cell5) {
                    cell5 = row.createCell(5);
                }
                cell5.setCellValue(d.getBarCode());// 订单号
                HSSFCell cell6 = row.getCell(6);
                if (null == cell6) {
                    cell6 = row.createCell(6);
                }
                cell6.setCellValue(d.getWeight());// 重量
                HSSFCell cell7 = row.getCell(7);
                if (null == cell7) {
                    cell7 = row.createCell(7);
                }
                cell7.setCellValue(d.getOWNER_NAME());// 收件人姓名
                HSSFCell cell8 = row.getCell(8);
                if (null == cell8) {
                    cell8 = row.createCell(8);
                }
                cell8.setCellValue(d.getOWNER_ADDR());// 收件人地址
                HSSFCell cell9 = row.getCell(9);
                if (null == cell9) {
                    cell9 = row.createCell(9);
                }
                cell9.setCellValue(d.getOWNER_TEL());// 收件人电话
                rowIndex++;
            }
            wb.write(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exportMyTable(JTable table, File file) throws IOException {
        TableModel model = table.getModel();
        BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));
        for (int i = 0; i < model.getColumnCount(); i++) {
            bWriter.write(model.getColumnName(i));
            bWriter.write("\t");
        }
        bWriter.newLine();
        for (int i = 0; i < model.getRowCount(); i++) {
            for (int j = 0; j < model.getColumnCount(); j++) {
                if (j == 1 || j == 2) {
                    bWriter.write(" " + model.getValueAt(i, j).toString());
                } else {
                    bWriter.write(model.getValueAt(i, j).toString());
                }
                bWriter.write("\t");
            }
            bWriter.newLine();
        }
        bWriter.close();
    }

    public void exportMy(File file, List<Content> data) {
        HSSFWorkbook wb = null;
        try {
            InputStream resourceAsStream = this.getClass().getResourceAsStream("/export/8111.xls");
            POIFSFileSystem fs = new POIFSFileSystem(resourceAsStream);
            // 读取excel模板
            wb = new HSSFWorkbook(fs);
            // 读取了模板内所有sheet内容
            HSSFSheet sheet = wb.getSheetAt(0);
            // 在相应的单元格进行赋值
            int rowIndex = 1;
            for (Content d : data) {
                HSSFRow row = sheet.getRow(rowIndex);
                if (null == row) {
                    row = sheet.createRow(rowIndex);
                }
                HSSFCell cell0 = row.getCell(0);
                if (null == cell0) {
                    cell0 = row.createCell(0);
                }
                cell0.setCellValue(rowIndex);// 序号
                HSSFCell cell1 = row.getCell(1);
                if (null == cell1) {
                    cell1 = row.createCell(1);
                }
                cell1.setCellValue(d.getVoyageNo());// 总运单号
                HSSFCell cell2 = row.getCell(2);
                if (null == cell2) {
                    cell2 = row.createCell(2);
                }
                cell2.setCellValue(d.getBarCode());// 运单号

                HSSFCell cell3 = row.getCell(3);
                if (null == cell3) {
                    cell3 = row.createCell(3);
                }
                cell3.setCellValue(d.getWeight());// 重量
                HSSFCell cell4 = row.getCell(4);
                if (null == cell4) {
                    cell4 = row.createCell(4);
                }
                cell4.setCellValue(d.getCustOrder().equals("0") ? "放行" : "查验");// 海关状态
                HSSFCell cell5 = row.getCell(5);
                if (null == cell5) {
                    cell5 = row.createCell(5);
                }
                cell5.setCellValue(d.getCtOrder() == null ? "未获取" : (d.getCtOrder().equals("C") ? "放行" : "嫌疑"));// CT嫌疑
                HSSFCell cell6 = row.getCell(6);
                if (null == cell6) {
                    cell6 = row.createCell(6);
                }
                cell6.setCellValue(d.getPackNo());// 件数
                HSSFCell cell7 = row.getCell(7);
                if (null == cell7) {
                    cell7 = row.createCell(7);
                }
                cell7.setCellValue(d.getMainGName());// 品名
                rowIndex++;
            }
            wb.write(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
